//
//  RCChangeOrderViewController.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-11-24.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCChangeOrderViewController.h"
#import "MBProgressHUD.h"
#import "RCChangeOrderInfoDataModel.h"
#import "UIColor-Expanded.h"
#import "NSString+NSString_Ex_.h"
#import "CommonDefines.h"
#import "UIImageView+WebCache.h"
#import "IManager+ModuleChangeOrderInfo.h"
#import "NSArray+EX.h"
#import "GloableEngineService.h"
#import "AppDelegate.h"
#import "RCFeedbackDataModel.h"
#import "IManager+ModuleChangeOrderCommit.h"

#define kImageHeaderImage               @"image_changeOrder_header_bg.png"
#define kImageForm1Image                @"image_changeOrder_form1.png"
#define kImageForm2Image                @"image_changeOrder_form2.png"
#define kImageText1Image                @"image_changeOrder_textView1.png"
#define kImageText2Image                @"image_changeOrder_textView2.png"
#define kButtonImageFile                @"login_btn.png"


#define kDatePickerCancelButton                @"image_tinyNote_edit_datePicker_cancel.png"
#define kDatePickerCancelPressed               @"image_tinyNote_edit_datePicker_cancel_pressed.png"
#define kDatePickerDoneButton                  @"image_tinyNote_edit_datePicker_done.png"
#define kDatePickerDonePressed                 @"image_tinyNote_edit_datePicker_done_pressed.png"
#define kDatePickerBGImageFile                 @"image_tinyNote_edit_datePicker_timebar.png"

#define kDatePickerBGFrame                     CGRectMake(0,UIScreenHeight-64-256,320,256)
#define kDatePickerButtonBGFrame               CGRectMake(0,0,320,40)
#define kDatePickerCancelFrame                 CGRectMake(20,5,50,30)
#define kDatePickerDoneFrame                   CGRectMake(250,5,50,30)
#define kDatePickerFrame                       CGRectMake(0,40,320,216)
#define kDatePickerUpperFrame                  CGRectMake(0,0,320,UIScreenHeight-64-256)

@interface RCChangeOrderViewController ()<UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource,MBProgressHUDDelegate,IManagerDelegate,UITextViewDelegate>

@property(nonatomic,copy)  NSString       *goodsID;

@property(nonatomic,retain)UIScrollView   *contentView;
@property(nonatomic,retain)UITextField    *nameTextField;
@property(nonatomic,retain)UITextField    *phoneTextField;

@property(nonatomic,retain)UILabel        *maleLabel;
@property(nonatomic,retain)UIView         *malePickerBGView;
@property(nonatomic,retain)UIImageView    *malePickerButtonBGView;
@property(nonatomic,retain)UIButton       *malePickerCancelButton;
@property(nonatomic,retain)UIButton       *malePickerDoneButton;
@property(nonatomic,retain)UIButton       *malePickerUpperButton;
@property(nonatomic,retain)UIPickerView   *malePickerView;
@property(nonatomic,copy)  NSString       *maleString;


@property(nonatomic,retain)UILabel        *changeCountLabel;
@property(nonatomic,retain)UIView         *changeCountPickerBGView;
@property(nonatomic,retain)UIImageView    *changeCountPickerButtonBGView;
@property(nonatomic,retain)UIButton       *changeCountPickerCancelButton;
@property(nonatomic,retain)UIButton       *changeCountPickerDoneButton;
@property(nonatomic,retain)UIButton       *changeCountPickerUpperButton;
@property(nonatomic,retain)UIPickerView   *changeCountPickerView;
@property(nonatomic,copy)  NSString       *changeCountString;

@property(nonatomic,assign)CGFloat        currentContentOffsetY;

@property(nonatomic,retain)RCChangeOrderInfoDataModel  *detailDataModel;

@property(nonatomic,retain)UITextView     *messageTextView;
@property(nonatomic,assign)TextViewType   textViewType;

@property(nonatomic,retain)UIButton *commitButton;
@property(nonatomic,retain)MBProgressHUDEx *progressHub;
@property(nonatomic,assign)int64_t requestID;
@property(nonatomic,assign)int64_t orderInfoRequestID;

@property(nonatomic,retain)NSMutableArray *maleArray;
@property(nonatomic,retain)NSMutableArray *changeNumberArray;

@end

@implementation RCChangeOrderViewController

@synthesize goodsID = _goodsID;
@synthesize contentView = _contentView;
@synthesize nameTextField = _nameTextField;
@synthesize phoneTextField = _phoneTextField;

@synthesize maleLabel = _maleLabel;
@synthesize malePickerBGView = _malePickerBGView;
@synthesize malePickerCancelButton = _malePickerCancelButton;
@synthesize malePickerDoneButton = _malePickerDoneButton;
@synthesize malePickerUpperButton = _malePickerUpperButton;
@synthesize malePickerButtonBGView = _malePickerButtonBGView;
@synthesize malePickerView = _malePickerView;
@synthesize maleString = _maleString;

@synthesize changeCountLabel = _changeCountLabel;
@synthesize changeCountPickerView = _changeCountPickerView;
@synthesize changeCountPickerBGView = _changeCountPickerBGView;
@synthesize changeCountPickerButtonBGView = _changeCountPickerButtonBGView;
@synthesize changeCountPickerCancelButton = _changeCountPickerCancelButton;
@synthesize changeCountPickerDoneButton = _changeCountPickerDoneButton;
@synthesize changeCountPickerUpperButton = _changeCountPickerUpperButton;
@synthesize changeCountString = _changeCountString;



@synthesize maleArray = _maleArray;
@synthesize changeNumberArray = _changeNumberArray;

@synthesize currentContentOffsetY = _currentContentOffsetY;
@synthesize detailDataModel = _detailDataModel;
@synthesize textViewType = _textViewType;

@synthesize messageTextView = _messageTextView;
@synthesize commitButton = _commitButton;
@synthesize progressHub = _progressHub;
@synthesize requestID = _requestID;
@synthesize orderInfoRequestID = _orderInfoRequestID;

@synthesize delegate = _delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
    self.delegate = nil;
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    self.goodsID = nil;
    self.commitButton = nil;
    self.messageTextView = nil;
    self.nameTextField = nil;
    self.phoneTextField = nil;
    
    self.malePickerView = nil;
    self.malePickerUpperButton = nil;
    self.malePickerCancelButton = nil;
    self.malePickerDoneButton = nil;
    self.malePickerButtonBGView = nil;
    self.malePickerBGView = nil;
    self.maleString = nil;
    self.maleLabel = Nil;
    
    self.changeCountPickerView = nil;
    self.changeCountPickerUpperButton = nil;
    self.changeCountPickerCancelButton = nil;
    self.changeCountPickerDoneButton = nil;
    self.changeCountPickerButtonBGView = nil;
    self.changeCountPickerBGView = nil;
    self.changeCountString = nil;
    self.changeCountLabel = nil;
    
    

    self.maleArray = nil;
    self.changeNumberArray = nil;
    

    self.detailDataModel = nil;
    self.contentView = nil;
    
    [super dealloc];
}

-(id)initWithGoodsID:(NSString *)goodsID{
    self = [super init];
    if (self) {
        self.goodsID = goodsID;
        self.maleArray = [NSMutableArray arrayWithObjects:@"男士",@"女士", nil];
        self.changeNumberArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50",@"51",@"52",@"53",@"54",@"55",@"56",@"57",@"58",@"59",@"60",@"61",@"62",@"63",@"64",@"65",@"66",@"67",@"68",@"69",@"70",@"71",@"72",@"73",@"74",@"75",@"76",@"77",@"78",@"79",@"80",@"81",@"82",@"83",@"84",@"85",@"86",@"87",@"88",@"89",@"90",@"91",@"92",@"93",@"94",@"95",@"96",@"97",@"98",@"99",@"100", nil];
        self.changeCountString = @"2";
        self.maleString = @"男士";
        [self initKeyBoard];
    }
    return self;
}

-(void)initKeyBoard{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

-(void)loadView{
    [super loadView];
    self.view.backgroundColor = [UIColor colorWithHex:0xfff9f9f9];
    self.titleLabelText = @"预订";
    self.navigationItem.titleView = [self createTitleView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self requestData];
}

-(void)requestData{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _orderInfoRequestID = [[IManager sharedInstance]requestChangeOrderInfo:self.goodsID withDelegate:self];
}

-(void)updateViews{
    
    _contentView = [[UIScrollView alloc]initWithFrame:self.viewContainer.bounds];
    _contentView.backgroundColor = [UIColor clearColor];
    
    
    UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 42)];
    headerImageView.backgroundColor = [UIColor clearColor];
    [headerImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kImageHeaderImage ofType:nil]]];
    CGFloat pointsWidth = 0;
    
    UILabel *pointsWordLabel = [[UILabel alloc]init];
    pointsWordLabel.backgroundColor = [UIColor clearColor];
    pointsWordLabel.textColor = [UIColor colorWithHex:0xff707070];
    pointsWordLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    pointsWordLabel.text = @"分";
    pointsWordLabel.textAlignment = UITextAlignmentRight;
    
    CGSize wordSize = [pointsWordLabel.text sizeWithFont:pointsWordLabel.font constrainedToSize:CGSizeMake(200, 20)];
    pointsWordLabel.frame = CGRectMake(307-wordSize.width, 15, wordSize.width, 12);
    [headerImageView addSubview:pointsWordLabel];
    [pointsWordLabel release];
    
    pointsWidth = 307-wordSize.width-2;
    
    UILabel *pointsLabel = [[UILabel alloc]init];
    pointsLabel.backgroundColor = [UIColor clearColor];
    pointsLabel.textColor = [UIColor colorWithHex:0xffff6900];
    pointsLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    pointsLabel.textAlignment = UITextAlignmentRight;
    pointsLabel.text = self.detailDataModel.mempoints;
    
    CGSize pointsSize = [pointsLabel.text sizeWithFont:pointsLabel.font constrainedToSize:CGSizeMake(200, 20)];
    pointsLabel.frame = CGRectMake(pointsWidth-pointsSize.width, 15, pointsSize.width, 12);
    [headerImageView addSubview:pointsLabel];
    [pointsLabel release];
    
    pointsWidth = pointsWidth-pointsSize.width-2;
    
    UILabel *pointsHeaderLabel = [[UILabel alloc]init];
    pointsHeaderLabel.backgroundColor = [UIColor clearColor];
    pointsHeaderLabel.textColor = [UIColor colorWithHex:0xff707070];
    pointsHeaderLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    pointsHeaderLabel.text = @"您目前有积分";
    pointsHeaderLabel.textAlignment = UITextAlignmentRight;
    
    CGSize headerSize = [pointsHeaderLabel.text sizeWithFont:pointsHeaderLabel.font constrainedToSize:CGSizeMake(200, 20)];
    pointsHeaderLabel.frame = CGRectMake(pointsWidth-headerSize.width, 15, headerSize.width, 12);
    [headerImageView addSubview:pointsHeaderLabel];
    [pointsHeaderLabel release];
    
    [_contentView addSubview:headerImageView];
    [headerImageView release];
    
    
    UILabel *goodsWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(23, 50, 230, 12)];
    goodsWordLabel.backgroundColor = [UIColor clearColor];
    goodsWordLabel.textColor = [UIColor colorWithHex:0xff505050];
    goodsWordLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    goodsWordLabel.text = @"商品名称：";
    
    CGSize goodsWordSize = [goodsWordLabel.text sizeWithFont:goodsWordLabel.font constrainedToSize:CGSizeMake(200, 20)];
    goodsWordLabel.frame = CGRectMake(23, 50, goodsWordSize.width, 12);
    [_contentView addSubview:goodsWordLabel];
    [goodsWordLabel release];
    
    UILabel *goodsNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(goodsWordLabel.frame), 50, 230, 12)];
    goodsNameLabel.backgroundColor = [UIColor clearColor];
    goodsNameLabel.textColor = [UIColor colorWithHex:0xff505050];
    goodsNameLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    goodsNameLabel.text = self.detailDataModel.shopName;
    [_contentView addSubview:goodsNameLabel];
    [goodsNameLabel release];
    
    CGFloat height = CGRectGetMaxY(goodsNameLabel.frame);
    
    if (![NSString stringIsNullOrEmpty:self.detailDataModel.thumb]) {
        height += 8;
        UIImageView *picImageView = [[UIImageView alloc]initWithFrame:CGRectMake(13, height, 294, 135)];
        picImageView.backgroundColor = [UIColor clearColor];
        [picImageView setImageWithURL:[NSURL URLWithString:self.detailDataModel.thumb] placeholderImage:nil];
        [_contentView addSubview:picImageView];
        [picImageView release];
        
        height +=135;
    }
    
    height +=11;
    
    //添加兑换数量
    UILabel *changeCountWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(23, height+3, 60, 12)];
    changeCountWordLabel.backgroundColor = [UIColor clearColor];
    changeCountWordLabel.textColor = [UIColor colorWithHex:0xff595959];
    changeCountWordLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    changeCountWordLabel.text = @"兑换数量：";
    [_contentView addSubview:changeCountWordLabel];
    [changeCountWordLabel release];
    
    
    UIButton *changeCountTextFiledImageView = [[UIButton alloc]initWithFrame:CGRectMake(90, height, 134, 18)];
    changeCountTextFiledImageView.backgroundColor = [UIColor clearColor];
    [changeCountTextFiledImageView setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kImageForm2Image ofType:nil]] forState:UIControlStateNormal];
    [changeCountTextFiledImageView addTarget:self action:@selector(changeCountTextButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    _changeCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 3, 110, 12)];
    _changeCountLabel.backgroundColor = [UIColor clearColor];
    _changeCountLabel.textColor = [UIColor colorWithHex:0xffff6700];
    _changeCountLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    _changeCountLabel.text = @"10";
    [changeCountTextFiledImageView addSubview:_changeCountLabel];
    [_contentView addSubview:changeCountTextFiledImageView];
    [changeCountTextFiledImageView release];
    
    height+=18;
    
    
    _changeCountPickerBGView = [[UIView alloc]initWithFrame:CGRectMake(0, UIScreenHeight-64-190, 320, 190)];
    _changeCountPickerBGView.backgroundColor = [UIColor whiteColor];
    [self.viewContainer addSubview:_changeCountPickerBGView];
    
    _changeCountPickerUpperButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 320, UIScreenHeight-64-190)];
    _changeCountPickerUpperButton.backgroundColor = [UIColor clearColor];
    [_changeCountPickerUpperButton addTarget:self action:@selector(changeCountPickerCalcelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    _changeCountPickerUpperButton.hidden = YES;
    [self.viewContainer addSubview:_changeCountPickerUpperButton];
    
    _changeCountPickerButtonBGView = [[UIImageView alloc]initWithFrame:kDatePickerButtonBGFrame];
    [_changeCountPickerButtonBGView setImage:[UIImage imageNamed:kDatePickerBGImageFile]];
    _changeCountPickerButtonBGView.userInteractionEnabled = YES;
    [_changeCountPickerBGView addSubview:_changeCountPickerButtonBGView];
    
    _changeCountPickerCancelButton = [[UIButton alloc]initWithFrame:kDatePickerCancelFrame];
    [_changeCountPickerCancelButton setBackgroundImage:[UIImage imageNamed:kDatePickerCancelButton] forState:UIControlStateNormal];
    [_changeCountPickerCancelButton setBackgroundImage:[UIImage imageNamed:kDatePickerCancelPressed] forState:UIControlStateSelected];
    [_changeCountPickerCancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [_changeCountPickerCancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _changeCountPickerCancelButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_changeCountPickerCancelButton addTarget:self action:@selector(changeCountPickerCalcelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_changeCountPickerButtonBGView addSubview:_changeCountPickerCancelButton];
    
    
    _changeCountPickerDoneButton = [[UIButton alloc]initWithFrame:kDatePickerDoneFrame];
    [_changeCountPickerDoneButton setBackgroundImage:[UIImage imageNamed:kDatePickerDoneButton] forState:UIControlStateNormal];
    [_changeCountPickerDoneButton setBackgroundImage:[UIImage imageNamed:kDatePickerDonePressed] forState:UIControlStateSelected];
    [_changeCountPickerDoneButton setTitle:@"完成" forState:UIControlStateNormal];
    [_changeCountPickerDoneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _changeCountPickerDoneButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_changeCountPickerDoneButton addTarget:self action:@selector(changeCountPickerDoneButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_changeCountPickerButtonBGView addSubview:_changeCountPickerDoneButton];
    _changeCountPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, 320, 150)];
    _changeCountPickerView.tag = 102;
    _changeCountPickerView.delegate = self;
    _changeCountPickerView.dataSource = self;
    _changeCountPickerView.showsSelectionIndicator = YES;
    [_changeCountPickerBGView addSubview:_changeCountPickerView];
    
    _changeCountPickerBGView.hidden = YES;
    
    
    height +=12;
    UILabel *requirePointsWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(23, height, 60, 12)];
    requirePointsWordLabel.backgroundColor = [UIColor clearColor];
    requirePointsWordLabel.textColor = [UIColor colorWithHex:0xff595959];
    requirePointsWordLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    requirePointsWordLabel.text = @"所需积分：";
    [_contentView addSubview:requirePointsWordLabel];
    [requirePointsWordLabel release];
    
    UILabel *requirePointsLabel = [[UILabel alloc]initWithFrame:CGRectMake(95, height, 100, 12)];
    requirePointsLabel.backgroundColor = [UIColor clearColor];
    requirePointsLabel.textColor = [UIColor colorWithHex:0xffff6700];
    requirePointsLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    requirePointsLabel.text = self.detailDataModel.goodspinots;
    [_contentView addSubview:requirePointsLabel];
    [requirePointsLabel release];
    
    height +=12;
    
    
    //添加预订用户信息
    height +=14;
    
    
    UILabel *contactLabel = [[UILabel alloc]initWithFrame:CGRectMake(23, height+3, 60, 12)];
    contactLabel.backgroundColor = [UIColor clearColor];
    contactLabel.textColor = [UIColor colorWithHex:0xff595959];
    contactLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    contactLabel.text = @"联系人：";
    [_contentView addSubview:contactLabel];
    [contactLabel release];
    
    UIImage *textFiledImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kImageText1Image ofType:nil]];
    
    UIImageView *nameTextFiledImageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, height, 134, 18)];
    nameTextFiledImageView.backgroundColor = [UIColor clearColor];
    nameTextFiledImageView.userInteractionEnabled = YES;
    [nameTextFiledImageView setImage:textFiledImage];
    
    
    _nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 0, 124, 18)];
    _nameTextField.backgroundColor = [UIColor clearColor];
    _nameTextField.delegate = self;
    _nameTextField.textColor = [UIColor colorWithHex:0xffb5b5b5];
    _nameTextField.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _nameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _nameTextField.placeholder = @"姓名";
    _nameTextField.tag = 201;
    [_nameTextField setReturnKeyType:UIReturnKeyDone];
    [nameTextFiledImageView addSubview:_nameTextField];
    [_contentView addSubview:nameTextFiledImageView];
    [nameTextFiledImageView release];
    
    UIButton *maleTextFiledImageView = [[UIButton alloc]initWithFrame:CGRectMake(222, height, 74, 18)];
    maleTextFiledImageView.backgroundColor = [UIColor clearColor];
    [maleTextFiledImageView setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kImageForm1Image ofType:nil]] forState:UIControlStateNormal];
    [maleTextFiledImageView addTarget:self action:@selector(maleTextButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    _maleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 3, 64, 12)];
    _maleLabel.backgroundColor = [UIColor clearColor];
    _maleLabel.textColor = [UIColor colorWithHex:0xffb5b5b5];
    _maleLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _maleLabel.textAlignment = UITextAlignmentLeft;
    _maleLabel.text = @"男士";
    [maleTextFiledImageView addSubview:_maleLabel];
    [_contentView addSubview:maleTextFiledImageView];
    [maleTextFiledImageView release];
    
    _malePickerBGView = [[UIView alloc]initWithFrame:CGRectMake(0, UIScreenHeight-64-190, 320, 190)];
    _malePickerBGView.backgroundColor = [UIColor whiteColor];
    [self.viewContainer addSubview:_malePickerBGView];
    
    _malePickerUpperButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 320, UIScreenHeight-64-190)];
    _malePickerUpperButton.backgroundColor = [UIColor clearColor];
    [_malePickerUpperButton addTarget:self action:@selector(malePickerCalcelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    _malePickerUpperButton.hidden = YES;
    [self.viewContainer addSubview:_malePickerUpperButton];
    
    _malePickerButtonBGView = [[UIImageView alloc]initWithFrame:kDatePickerButtonBGFrame];
    [_malePickerButtonBGView setImage:[UIImage imageNamed:kDatePickerBGImageFile]];
    _malePickerButtonBGView.userInteractionEnabled = YES;
    [_malePickerBGView addSubview:_malePickerButtonBGView];
    
    _malePickerCancelButton = [[UIButton alloc]initWithFrame:kDatePickerCancelFrame];
    [_malePickerCancelButton setBackgroundImage:[UIImage imageNamed:kDatePickerCancelButton] forState:UIControlStateNormal];
    [_malePickerCancelButton setBackgroundImage:[UIImage imageNamed:kDatePickerCancelPressed] forState:UIControlStateSelected];
    [_malePickerCancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [_malePickerCancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _malePickerCancelButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_malePickerCancelButton addTarget:self action:@selector(malePickerCalcelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_malePickerButtonBGView addSubview:_malePickerCancelButton];
    
    
    _malePickerDoneButton = [[UIButton alloc]initWithFrame:kDatePickerDoneFrame];
    [_malePickerDoneButton setBackgroundImage:[UIImage imageNamed:kDatePickerDoneButton] forState:UIControlStateNormal];
    [_malePickerDoneButton setBackgroundImage:[UIImage imageNamed:kDatePickerDonePressed] forState:UIControlStateSelected];
    [_malePickerDoneButton setTitle:@"完成" forState:UIControlStateNormal];
    [_malePickerDoneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _malePickerDoneButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_malePickerDoneButton addTarget:self action:@selector(malePickerDoneButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_malePickerButtonBGView addSubview:_malePickerDoneButton];
    _malePickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, 320, 200)];
    _malePickerView.tag = 101;
    _malePickerView.delegate = self;
    _malePickerView.dataSource = self;
    _malePickerView.showsSelectionIndicator = YES;
    [_malePickerBGView addSubview:_malePickerView];
    _malePickerBGView.hidden = YES;
    
    height +=18;
    
    height +=8;
    
    UILabel *phoneTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(23, height+3, 60, 12)];
    phoneTextLabel.backgroundColor = [UIColor clearColor];
    phoneTextLabel.textColor = [UIColor colorWithHex:0xff595959];
    phoneTextLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    phoneTextLabel.text = @"联系电话：";
    [_contentView addSubview:phoneTextLabel];
    [phoneTextLabel release];
    
    UIImageView *phoneTextFiledImageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, height, 134, 18)];
    phoneTextFiledImageView.backgroundColor = [UIColor clearColor];
    phoneTextFiledImageView.userInteractionEnabled = YES;
    [phoneTextFiledImageView setImage:textFiledImage];
    
    
    _phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 0, 124, 18)];
    _phoneTextField.backgroundColor = [UIColor clearColor];
    _phoneTextField.delegate = self;
    _phoneTextField.textColor = [UIColor colorWithHex:0xffb5b5b5];
    _phoneTextField.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    _phoneTextField.textAlignment = UITextAlignmentLeft;
    _phoneTextField.tag = 202;
    _phoneTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [_phoneTextField setReturnKeyType:UIReturnKeyDone];
    [phoneTextFiledImageView addSubview:_phoneTextField];
    [_contentView addSubview:phoneTextFiledImageView];
    [phoneTextFiledImageView release];
    height +=18;
    
    height +=8;
    UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(23, height+3, 60, 12)];
    messageLabel.backgroundColor = [UIColor clearColor];
    messageLabel.textColor = [UIColor colorWithHex:0xff595959];
    messageLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    messageLabel.text = @"留言";
    [_contentView addSubview:messageLabel];
    [messageLabel release];
    
    
    UIImageView *messageTextFiledImageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, height, 212, 86)];
    messageTextFiledImageView.backgroundColor = [UIColor clearColor];
    messageTextFiledImageView.userInteractionEnabled = YES;
    [messageTextFiledImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kImageText2Image ofType:nil]]];
    
    
    _messageTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 212, 86)];
    _messageTextView.backgroundColor = [UIColor clearColor];
    _messageTextView.delegate = self;
    _messageTextView.textColor = [UIColor colorWithHex:0xffb5b5b5];
    _messageTextView.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    _messageTextView.textAlignment = UITextAlignmentLeft;
    [_messageTextView setReturnKeyType:UIReturnKeyDone];
    [messageTextFiledImageView addSubview:_messageTextView];
    [_contentView addSubview:messageTextFiledImageView];
    [messageTextFiledImageView release];
    height +=86;
    
    height +=36;
    _commitButton = [[UIButton alloc]initWithFrame:CGRectMake(14, height, 292, 35)];
    _commitButton.backgroundColor = [UIColor clearColor];
    [_commitButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kButtonImageFile ofType:nil]] forState:UIControlStateNormal];
    [_commitButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kButtonImageFile ofType:nil]] forState:UIControlStateHighlighted];
    _commitButton.titleLabel.font = [UIFont fontWithName:kFontNameHeiti size:16];
    [_commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [_commitButton setTitle:@"确认" forState:UIControlStateNormal];
    [_commitButton setTitle:@"确认" forState:UIControlStateHighlighted];
    [_commitButton addTarget:self action:@selector(commitButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_commitButton];
    
    _contentView.contentSize = CGSizeMake(320, CGRectGetMaxY(_commitButton.frame)+19);
    
    [self.viewContainer addSubview:_contentView];
}


#pragma -mark UITextFeildDelegate

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField.tag == 201) {
        self.textViewType = TextViewTypeName;
    }else{
        self.textViewType = TextViewTypePhone;
    }
    return YES;
}

#pragma -mark UITextViewDelegate

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    self.textViewType = TextViewTypeMessage;
    return YES;
}

#pragma UIPickerViewDelegate

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (pickerView.tag == 101) {
        return [_maleArray count];
    }else if(pickerView.tag == 102){
        return [_changeNumberArray count];
    }
    
    return 0;
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (pickerView.tag == 101) {
        NSString *str = nil;
        str = [_maleArray safeObjectAtIndex:row];
        if (nil == str) {
            str = @"";
        }
        return str;
    }else if(pickerView.tag == 102){
        NSString *str = nil;
        str = [_changeNumberArray safeObjectAtIndex:row];
        if (nil == str) {
            str = @"";
        }
        return str;
    }else{
       
        return nil;
    }
    
    return nil;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (pickerView.tag == 101) {
        NSString *str = nil;
        str = [_maleArray safeObjectAtIndex:row];
        if (nil == str) {
            str = @"";
        }
        self.maleString = str;
    }else if(pickerView.tag == 102){
        NSString *str = nil;
        str = [_changeNumberArray safeObjectAtIndex:row];
        if (nil == str) {
            str = @"";
        }
        self.changeCountString = str;
    }
}


#pragma mark- malePicker event
- (void) maleTextButtonClicked:(id)sender{
    
    UIButton *button = (UIButton *)sender;
    
    if (_malePickerBGView.hidden == NO) {
        _malePickerBGView.hidden = YES;
        
    }else{
        _malePickerBGView.hidden = NO;
        [self.viewContainer bringSubviewToFront:_malePickerBGView];
        _malePickerUpperButton.hidden = NO;
        [self.viewContainer bringSubviewToFront:_malePickerUpperButton];
        
        CGFloat buttonOriginY = CGRectGetMaxY(button.frame);
        CGFloat bgViewOriginY = _malePickerBGView.frame.origin.y;
        
        CGFloat contentOffset = _contentView.contentOffset.y;
        self.currentContentOffsetY = contentOffset;
        
        if (buttonOriginY+5-contentOffset > bgViewOriginY) {
            _contentView.contentOffset = CGPointMake(0, (buttonOriginY+5-bgViewOriginY));
        }
        
    }
}

- (void)malePickerCalcelButtonClicked{
    _malePickerBGView.hidden = YES;
    _malePickerUpperButton.hidden = YES;
    _contentView.contentOffset = CGPointMake(0, self.currentContentOffsetY);
}

- (void)malePickerDoneButtonClicked{
    
    _maleLabel.text = self.maleString;
    _malePickerBGView.hidden = YES;
    _malePickerUpperButton.hidden = YES;
    _contentView.contentOffset = CGPointMake(0, self.currentContentOffsetY);
    
}

#pragma mark- peoplePicker event
- (void) changeCountTextButtonClicked:(id)sender{
    
    UIButton *button = (UIButton *)sender;
    
    if (_changeCountPickerBGView.hidden == NO) {
        _changeCountPickerBGView.hidden = YES;
        
    }else{
        _changeCountPickerBGView.hidden = NO;
        [self.viewContainer bringSubviewToFront:_changeCountPickerBGView];
        _changeCountPickerUpperButton.hidden = NO;
        [self.viewContainer bringSubviewToFront:_changeCountPickerUpperButton];
        
        CGFloat buttonOriginY = CGRectGetMaxY(button.frame);
        CGFloat bgViewOriginY = _changeCountPickerBGView.frame.origin.y;
        
        CGFloat contentOffset = _contentView.contentOffset.y;
        self.currentContentOffsetY = contentOffset;
        
        if (buttonOriginY+5-contentOffset > bgViewOriginY) {
            _contentView.contentOffset = CGPointMake(0, (buttonOriginY+5-bgViewOriginY));
        }
        
    }
}

- (void)changeCountPickerCalcelButtonClicked{
    _changeCountPickerBGView.hidden = YES;
    _changeCountPickerUpperButton.hidden = YES;
    _contentView.contentOffset = CGPointMake(0, self.currentContentOffsetY);
}

- (void)changeCountPickerDoneButtonClicked{
    
    _changeCountLabel.text = self.changeCountString;
    _changeCountPickerBGView.hidden = YES;
    _changeCountPickerUpperButton.hidden = YES;
    _contentView.contentOffset = CGPointMake(0, self.currentContentOffsetY);
    
}

-(void)commitButtonClicked{
    
    _commitButton.enabled = NO;
    
    NetworkStatus status = [GloableEngineService sharedInstance].netWorkStatus;
    if (status == kNotReachable) {
        [RCAppDelegate alertWithNetworkError];
        _commitButton.enabled = YES;
        return;
    }
    if (nil == _nameTextField.text || [_nameTextField.text isEqualToString:@""] || [_nameTextField.text isEqualToString:@"姓名"]) {
        [RCAppDelegate alertWithCustomizeString:@"请填写您姓名！"];
        _commitButton.enabled = YES;
        return;
    }
    
    if (nil == _phoneTextField.text || [_phoneTextField.text isEqualToString:@""]) {
        [RCAppDelegate alertWithCustomizeString:@"请填写联系电话"];
        _commitButton.enabled = YES;
        return;
    }
    
    NSString *phone = _phoneTextField.text;
    
    NSError * error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^1[34578]\\d{9}$" options:0 error:&error];
    NSTextCheckingResult * result = [regex firstMatchInString:phone options:0 range:NSMakeRange(0, [phone length])];
    if (!result || (regex.numberOfCaptureGroups + 1) != result.numberOfRanges) {
        [RCAppDelegate alertWithCustomizeString:@"手机号码输入格式错误哦！"];
        _commitButton.enabled = YES;
        return;
    }
    
    NSString *content = _messageTextView.text;
    
    NSString *name = _nameTextField.text;
    
    NSString *num = _changeCountLabel.text;
    
    
//    NSString *sex = nil;
//    if ([_maleLabel.text isEqualToString:@"男士"]) {
//        sex = @"1";
//    }else{
//        sex = @"2";
//    }
    
    
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"提交中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];

    _requestID = [[IManager sharedInstance]requestChangeCommitOrder:self.goodsID withName:name withContent:content withPhone:phone withChangeNum:num withDelegate:self];
    
}

-(void)updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        RCFeedbackDataModel *feedbackDataModel = (RCFeedbackDataModel *)dataModel;
        if (feedbackDataModel.errorNO == 0) {
            [RCAppDelegate alertWithCustomizeString:@"提交成功"];
            _commitButton.enabled = NO;
            
            if (_delegate && [_delegate respondsToSelector:@selector(orderCommitSuccess)]) {
                [_delegate orderCommitSuccess];
            }
            
            [self performSelector: @selector(leftButtonClicked) withObject: nil afterDelay: 1.5f];
        }else{
            [RCAppDelegate alertWithCustomizeString:@"提交失败，请稍后重试"];
            _commitButton.enabled = YES;
        }
    }else if (_orderInfoRequestID == requestId){
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        self.detailDataModel = (RCChangeOrderInfoDataModel *)dataModel;
        [self updateViews];
    }
}

-(void)leftButtonClicked{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        [RCAppDelegate alertWithCustomizeString:@"提交失败，请稍后重试"];
        _commitButton.enabled = YES;
    }else if (_orderInfoRequestID == requestId){
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
    }
}

#pragma mark -
#pragma mark Responding to keyboard events
- (void)keyboardWillShow:(NSNotification *)notification {
    
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [value CGRectValue];
    
    CGFloat keyboardOriginY = UIScreenHeight-64-keyboardRect.size.height;
    
    CGFloat textMaxY = 0;
    switch (self.textViewType) {
        case TextViewTypePhone:
            textMaxY = CGRectGetMaxY(_phoneTextField.superview.frame);
            break;
        case TextViewTypeName:
            textMaxY = CGRectGetMaxY(_nameTextField.superview.frame);
            break;
        case TextViewTypeMessage:
            textMaxY = CGRectGetMaxY(_messageTextView.superview.frame);
        default:
            break;
    }
    
    CGFloat contentOffsetY = self.contentView.contentOffset.y;
    
    if (textMaxY + 5 - contentOffsetY > keyboardOriginY) {
        self.currentContentOffsetY = contentOffsetY;
        
        [UIView animateWithDuration:0.3f animations:^{
            CGFloat heightReduce = abs(textMaxY +5 - keyboardOriginY);
            self.contentView.contentOffset = CGPointMake(0, heightReduce);
        }];
        
    }
    
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3f animations:^{
        self.contentView.contentOffset = CGPointMake(0, self.currentContentOffsetY);
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
